package com.allprs.hmcs.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.allprs.common.core.domain.AjaxResult;
import com.allprs.common.utils.StringUtils;
import com.allprs.hmcs.entity.ChartData;
import com.allprs.hmcs.mapper.ChartDataMapper;
import com.allprs.hmcs.pojo.resBo.ChartDataQueryBo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author wjf
 * @描述:
 * @date 2024年08月09日
 * @version: 1.0
 */

@RestController
@RequestMapping("/chart")
@Api(tags = "图表数据集")
@Slf4j
public class ChartController {

    @Resource
    private ChartDataMapper chartDataMapper;

    @ApiOperation(value = "根据一级key和二级key查询图表数据")
    @GetMapping("/getChartData")
    public AjaxResult getChartData(ChartDataQueryBo queryBo){
        ChartData chartData = chartDataMapper.selectById(1L);

        String content = chartData.getContent();

        String firstKey = queryBo.getFirstKey();

        String secondKey = queryBo.getSecondKey();

        JSONObject contentjSON = JSON.parseObject(content);

        if(StringUtils.isBlank(firstKey)){
            return AjaxResult.success("ok",contentjSON);
        }

        JSONObject jsonObject = contentjSON.getJSONObject(firstKey);

        if(StringUtils.isNotBlank(secondKey)){
            Object result = jsonObject.get(secondKey);
            return AjaxResult.success("ok",result);
        }else{
            return AjaxResult.success("ok",jsonObject);
        }

    }
}
